package display;

import java.awt.BorderLayout;
import java.awt.FlowLayout;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import java.awt.GridBagLayout;
import javax.swing.JLabel;
import java.awt.GridBagConstraints;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;
import java.text.DateFormat;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Collections;
import java.sql.Date;

import javax.swing.JComboBox;
import javax.swing.DefaultComboBoxModel;

import javax.swing.JTextField;

import Util.Util;

import term.Term;
import term.Term.Semester;

import database.Database;

public class NewTermDialog extends JDialog {

	private final JPanel contentPanel = new JPanel();
	private JComboBox semesters, year;
	private InventoryData inv = null;
	private MyActionListener al = new MyActionListener();
	private Database db;
	private JTextField sDate;
	private JTextField eDate;

	/**
	 * Create the dialog.
	 */
	public NewTermDialog(InventoryData in, Database d) {
		inv = in;
		db = d;
		setBounds(100, 100, 253, 184);
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		GridBagLayout gbl_contentPanel = new GridBagLayout();
		gbl_contentPanel.columnWidths = new int[] { 0, 0, 0 };
		gbl_contentPanel.rowHeights = new int[] { 0, 0, 0, 0, 0 };
		gbl_contentPanel.columnWeights = new double[] { 6.0, 1.0,
				Double.MIN_VALUE };
		gbl_contentPanel.rowWeights = new double[] { 0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE };
		contentPanel.setLayout(gbl_contentPanel);

		JLabel lblSemester = new JLabel("Semester");
		GridBagConstraints gbc_lblSemester = new GridBagConstraints();
		gbc_lblSemester.insets = new Insets(0, 0, 5, 5);
		gbc_lblSemester.gridx = 0;
		gbc_lblSemester.gridy = 0;
		contentPanel.add(lblSemester, gbc_lblSemester);
		JLabel lblYear = new JLabel("Year");
		GridBagConstraints gbc_lblYear = new GridBagConstraints();
		gbc_lblYear.insets = new Insets(0, 0, 5, 0);
		gbc_lblYear.gridx = 1;
		gbc_lblYear.gridy = 0;
		contentPanel.add(lblYear, gbc_lblYear);
		semesters = new JComboBox();
		semesters.setModel(new DefaultComboBoxModel(Semester.values()));
		semesters.addActionListener(al);
		
		GridBagConstraints gbc_semesters = new GridBagConstraints();
		gbc_semesters.insets = new Insets(0, 0, 5, 5);
		gbc_semesters.fill = GridBagConstraints.HORIZONTAL;
		gbc_semesters.gridx = 0;
		gbc_semesters.gridy = 1;
		contentPanel.add(semesters, gbc_semesters);
		year = new JComboBox();
		year.setEditable(true);
		
		ArrayList<Integer> yearList = new ArrayList<Integer>();
		for (Term t : inv.getTerms()) {
			if (t.getSemester().equals(semesters.getSelectedItem()))
				yearList.add(t.getYear());
		}
		
		year.setModel(new DefaultComboBoxModel(yearList.toArray()));
		
		GridBagConstraints gbc_year = new GridBagConstraints();
		gbc_year.insets = new Insets(0, 0, 5, 0);
		gbc_year.fill = GridBagConstraints.HORIZONTAL;
		gbc_year.gridx = 1;
		gbc_year.gridy = 1;
		contentPanel.add(year, gbc_year);
		
		JLabel lblStartDatemmddyy = new JLabel("Start Date");
		GridBagConstraints gbc_lblStartDatemmddyy = new GridBagConstraints();
		gbc_lblStartDatemmddyy.anchor = GridBagConstraints.EAST;
		gbc_lblStartDatemmddyy.insets = new Insets(0, 0, 5, 5);
		gbc_lblStartDatemmddyy.gridx = 0;
		gbc_lblStartDatemmddyy.gridy = 2;
		contentPanel.add(lblStartDatemmddyy, gbc_lblStartDatemmddyy);
		
		sDate = new JTextField();
		sDate.setText("mm/dd/yy");
		GridBagConstraints gbc_sDate = new GridBagConstraints();
		gbc_sDate.insets = new Insets(0, 0, 5, 0);
		gbc_sDate.fill = GridBagConstraints.HORIZONTAL;
		gbc_sDate.gridx = 1;
		gbc_sDate.gridy = 2;
		contentPanel.add(sDate, gbc_sDate);
		sDate.setColumns(10);
		
		JLabel lblEndDatemmddyy = new JLabel("End Date");
		GridBagConstraints gbc_lblEndDatemmddyy = new GridBagConstraints();
		gbc_lblEndDatemmddyy.anchor = GridBagConstraints.EAST;
		gbc_lblEndDatemmddyy.insets = new Insets(0, 0, 0, 5);
		gbc_lblEndDatemmddyy.gridx = 0;
		gbc_lblEndDatemmddyy.gridy = 3;
		contentPanel.add(lblEndDatemmddyy, gbc_lblEndDatemmddyy);
		
		eDate = new JTextField();
		eDate.setText("mm/dd/yy");
		eDate.setColumns(10);
		GridBagConstraints gbc_eDate = new GridBagConstraints();
		gbc_eDate.fill = GridBagConstraints.HORIZONTAL;
		gbc_eDate.gridx = 1;
		gbc_eDate.gridy = 3;
		contentPanel.add(eDate, gbc_eDate);
		
		JPanel buttonPane = new JPanel();
		buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
		getContentPane().add(buttonPane, BorderLayout.SOUTH);
		JButton okButton = new JButton("OK");
		okButton.setActionCommand("OK");
		okButton.addActionListener(al);
		buttonPane.add(okButton);
		getRootPane().setDefaultButton(okButton);
		JButton cancelButton = new JButton("Cancel");
		cancelButton.setActionCommand("Cancel");
		cancelButton.addActionListener(al);
		buttonPane.add(cancelButton);
	}
	
	private class MyActionListener implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent arg0) {
			
			if (arg0.getActionCommand().equalsIgnoreCase("ok")) {
				// TODO Auto-generated method stub
				Semester sem = (Semester)semesters.getSelectedItem();
				
				Object yearObj = year.getSelectedItem();
				int y;
				if (yearObj instanceof Integer)
					y = (Integer) yearObj;
				else if (yearObj instanceof String)
					y = Integer.valueOf((String)yearObj);
				else {
					Util.printWarningString("What is this: "+yearObj.getClass().toString());
					y = 2012;
				}
				Term t;
				if(sem.equals(Semester.FALL))
					t = Term.getFall(y);
				else if (sem.equals(Semester.SPRING))
					t = Term.getSpring(y);
				else if (sem.equals(Semester.SUMMER_A))
					t = Term.getSummerA(y);
				else
					t = Term.getSummerB(y);
				
				t = db.getTerm(t);
				
				Date start,end;
				try {
					DateFormat df = DateFormat.getDateInstance(DateFormat.SHORT);
					start = new Date(df.parse(sDate.getText()).getTime());
					end = new Date(df.parse(eDate.getText()).getTime());
					t.setDates(start, end);
				} catch (ParseException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				inv.selectTerm(t);
				NewTermDialog.this.dispose();
			}
			else if (arg0.getActionCommand().equalsIgnoreCase("cancel"))
				NewTermDialog.this.dispose();
			else {
				ArrayList<Integer> yearList = new ArrayList<Integer>();
				year.removeAllItems();
				for (Term t : inv.getTerms()) {
					if (t.getSemester().equals(semesters.getSelectedItem()))
						yearList.add(t.getYear());
				}
				year.setModel(new DefaultComboBoxModel(yearList.toArray()));
			}
		}
		
	}

}
